package com.david.utils;

import com.alibaba.fastjson.JSONObject;
import com.david.model.api.AdminCookie;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class WebAuthentication {
    private static String WebsiteAuthKey = "c9w324hh";
    public static String Java_AuthCookieName = "davidAuthTokenV1";
    private static String davidSecretKey = "18b2289cf24193c55ef51240ffa07414006ee7c3e9375e9a0d51327004ef22d7";//密码密钥
    private static WebAuthentication webAuthentication;

    /**
     * 用户密码加密
     *
     * @param data 需要加密数据
     * @return 密文
     * @throws Exception
     */
    public static String encryptionData(String data) throws Exception {
        return DesUtil.sha256Encrypt(DesUtil.md5(data) + davidSecretKey);
    }

    /**
     * www网站用户登录Cookie加密
     *
     * @param cookie
     * @return
     * @throws Exception
     */
    public static String EncryptLoginCookieForWebsite(String cookie) throws Exception {
        return DesUtil.encrypt(cookie, WebsiteAuthKey);
    }


    /**
     * www网站用户登录Cookie解密
     * @param cookie
     * @return
     * @throws Exception
     */
    public static String DecryptLoginCookieForWebsite(String cookie ) throws Exception {
        return DesUtil.decrypt(cookie, WebsiteAuthKey);
    }

    /**
     * www网站登录方法
     *
     * @param userName 用户名
     * @param userId   用户ID
     * @throws Exception
     */
    public static void SignIn(String userName, Integer userId) throws Exception {
        if (userId != null && userId > 0 && userName != null && !userName.isEmpty()) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            AdminCookie userCookie = new AdminCookie();
            userCookie.setUserName(userName);
            userCookie.setUserId(userId);
            userCookie.setLoginIp(IPUtils.getIpAddr(request));
            String cookieStr = JSONObject.toJSONString(userCookie);
            CookieUtils.removeCookie(Java_AuthCookieName);
            CookieUtils.addCookieString(Java_AuthCookieName, EncryptLoginCookieForWebsite(cookieStr));

            CookieUtils.addCookie("DisplayAdminName", "/", 86400, userName);
        }
    }

    /**
     * 检查登录cookie是否合规
     * @param cookie
     * @return
     * @throws Exception
     */
    public static Boolean CheckLoginCookie(String cookie) throws Exception {
        if (cookie != null && !cookie.isEmpty()) {
            String value = DecryptLoginCookieForWebsite(cookie);
            AdminCookie user = JSONObject.parseObject(value, AdminCookie.class);
            return user != null
                    && user.getUserId() > 0
                    && !user.getUserName().isEmpty()//登陆信息验证
                    && ((user.getLoginIp().equals(IPUtils.getIpAddr()))
                    || user.getLoginIp().equals("127.0.0.1")
                    || user.getLoginIp().indexOf("192.168") == 0);
        }
        return false;
    }


    /**
     * 删除登录缓存
     * @throws Exception
     */
    public static void signOut() throws Exception {
        CookieUtils.removeCookie(Java_AuthCookieName);
    }

}
